package com.ytzl.common;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.omg.CORBA.INTERNAL;

import java.util.Calendar;
import java.util.Map;

public class JwtUtils {
    //签名
    private static final String SIGN = "j!@($fjri%&f%#0$#";
    //过期时间
    private static final Integer EXPIRE_TIME = 7;

    //创建token
    public static String createToken(Map<String,String> map){
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,EXPIRE_TIME);

        JWTCreator.Builder builder = JWT.create();

        //payload
        map.forEach((k,v) ->{
            builder.withClaim(k,v);
        });

        return builder.withExpiresAt(instance.getTime()).sign(Algorithm.HMAC256(SIGN));
    }

    //验证token
    public static DecodedJWT verifyToken(String token){
        return JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
    }
}
